package com.example.blogdemo.service;

import com.example.blogdemo.entity.Article;
import com.example.blogdemo.entity.User;
import com.example.blogdemo.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author Anita
 * @Date 2017/12/13 17:32
 */
@Service
public class ArticleService {
    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int addArticle(Article article){
        return articleRepository.save(article).getId();
    }

    public Article getArticleById(Integer articleId){
        return articleRepository.findById(articleId);
    }

    public int getArticleCount(){
        return articleRepository.findArticleCount();
    }

    public int getArticleCountByCategory(String category){
        return articleRepository.findArticleCountByCategory(category);
    }

    public List<Article> getLatestArticles(int offset,int limit){
        PageRequest request = new PageRequest(offset,limit);
        return articleRepository.findAll(request).getContent();
    }

    public List<Article> getArticlesByCategory(String category, int offset, int limit){
        PageRequest request = new PageRequest(offset,limit);
        return articleRepository.findByCategory(category,request).getContent();
    }

    public List<Article> getArticlesByCategoryAndUserId(String category,Integer userId){
        return articleRepository.findByCategoryAndUserId(category,userId);
    }

    public List<Article> getArticlesByCategoryAndUserId(String category,Integer userId,int offset,int limit){
        PageRequest request = new PageRequest(offset,limit);
        return articleRepository.findByCategoryAndUserId(category,userId,request).getContent();
    }


    public void updateCommentCount(Integer id, Integer commentCount){
        articleRepository.updateCommentCountByQuestionId(commentCount,id);
    }

    public Integer getArticleCountByTag(int tagId){

        String querySql = String.format("select count(id) from article where id in (select article_id from article_tag where tag_id =%d)",tagId);

        return jdbcTemplate.queryForObject(querySql,Integer.class);
    }

    public List<Article> getArticlesByTag(int tagId,int offset, int limit){
        String querySql = String.format("select  id, title, describes, content, created_date, comment_count , category from article where id in (select article_id from article_tag where tag_id= %d )limit %d,%d",tagId,offset,limit);
        RowMapper<Article> rowMapper = new BeanPropertyRowMapper<>(Article.class);
        return jdbcTemplate.query(querySql,rowMapper);
    }

    public List<Article> getArticlesByUserId(Integer userId,int offset,int limit){
        PageRequest pageRequest = new PageRequest(offset,limit);
        return articleRepository.findByUserId(userId,pageRequest).getContent();
    }

    public List<Article> getArticlesByUserId(Integer userId){
        return articleRepository.findByUserId(userId);
    }

    public List<Article> getArticleByTagAndUserId(int tagId,Integer userId,int offset,int limit){
        String querySql = String.format("select  id, title, describes, content, created_date, comment_count , category from article where id in (select article_id from article_tag where tag_id= %d ) and user_Id = %d limit %d,%d",tagId,userId,offset,limit);
        RowMapper<Article> rowMapper = new BeanPropertyRowMapper<>(Article.class);
        return jdbcTemplate.query(querySql,rowMapper);
    }



}
